import cv2

# 读取彩色图像
image = cv2.imread('../photo2.jpeg')

if image is None:
    print("无法读取图像，请检查图像路径。")
else:
    # 将图像转换为YCrCb色彩空间
    ycrcb_image = cv2.cvtColor(image, cv2.COLOR_BGR2YCrCb)

    # 分离Y, Cr, Cb通道
    y, cr, cb = cv2.split(ycrcb_image)

    # 对Y通道进行直方图均衡化
    equalized_y = cv2.equalizeHist(y)

    # 合并通道
    equalized_ycrcb = cv2.merge((equalized_y, cr, cb))

    # 将图像转换回BGR色彩空间
    equalized_image = cv2.cvtColor(equalized_ycrcb, cv2.COLOR_YCrCb2BGR)

    # 显示原始图像和均衡化后的图像
    cv2.imshow('Original Image', image)
    cv2.imshow('Equalized Image', equalized_image)

    # 等待按键关闭窗口
    cv2.waitKey(0)
    cv2.destroyAllWindows()